package com.medicine.ecg.service;

import com.medicine.ecg.mapper.PatientMapper;
import com.medicine.pojo.Element;
import com.medicine.pojo.Patient;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ChenJie
 * @date 2021/5/24
 */

@Service
public class PatientService {

    @Autowired
    private PatientMapper patientMapper;

    public Patient getPatientById(int id){
        return patientMapper.queryPatientById(id);
    }

    public List<Patient> getPatientList(){
        return patientMapper.queryPatientList();
    }


    //按照姓名实现模糊查询
    public List<Patient> getPatientByName(String name){
        return patientMapper.queryPatientByName(name);
    }

    // 按id获取患者信息包括基本信息和心电图信息
    public Patient getPatientInfoById(int id)  {
        Patient patient = patientMapper.queryPatientInfoById(id);
        if(patient != null){
            readAddress(patient.getElectrocardios());
        }
        return patient;
    }
//        获取默认id为1的患者信息
    public Patient getEcgInfo(){
        Patient patient = patientMapper.queryEcgInfoDefault();
        if(patient != null){
            readAddress(patient.getElectrocardios());
        }
        return patient;
    }
//    获取id为1的诊断结果信息
    public Map<String,Object> getEcgResult(){
        return patientMapper.queryEcgResult();
    }

//    获取患者诊断结果
    public Map<String,Object> getEcgDiagnosis(Integer id){
        return patientMapper.queryEcgDiagnosis(id);
    }



    //    获取患者异常心电信息
    public List<Map<String , Object>> getEcgExceptionById(int id){
    //        存放异常信息的集合
        List<Map<String , Object>> excpsList = patientMapper.queryEcgExceptionById(id);
    //        遍历异常信息集合
        excpsList.forEach(ecp->{
    //         获取异常心电信息并取出坐标
            String coordinate = ecp.get("coordinate").toString();
            int i = coordinate.indexOf(',');
            Double xDotStart = Double.valueOf(coordinate.substring(1, i));
            Double xDotEnd = Double.valueOf(coordinate.substring(i+1,coordinate.length()-1));
    //            根据getIndex方法计算出异常心电坐标的索引
            int indexStart = getIndex(getArrayX(), xDotStart);
            int indexEnd = getIndex(getArrayX(), xDotEnd);
            ecp.put("begin",indexStart);
            ecp.put("end",indexEnd);
            Double[] arrCoordinate = new Double[]{xDotStart,xDotEnd};
            ecp.put("arrCoordinate",arrCoordinate);
        });

        return excpsList ;
    }
//    获取element信息
    public List<Element> getElementInfo(){
        return patientMapper.queryElementInfo();
    }


    //	获取异常段的起始坐标的索引
    public static int getIndex(Double[] arrX, double value ){
        for(int i=0 ; i < arrX.length ; i++){
            if(arrX[i] == value ){
                return i+1;
            }
        }
        return -1;
    }
    //    按id获取患者心电图信息
    public List<Map<String ,Object>> getEcgInfoById(int id){
        return readAddress(patientMapper.queryEcgInfoById(id));
    }

        //        计算x坐标点
    public  Double[] getArrayX(){
        Double[] arrX = new Double[3600];
        for(int i=0 ; i <=3599 ;i++){
            double xDot = i/(double)360;
            arrX[i] = (double) Math.round(xDot*100000)/100000;
        }
        return arrX;
    }
//        遍历集合，取出地址并读取文件中的excel表格
    public List<Map<String, Object>> readAddress(List<Map<String ,Object>> ra) {
        ra.forEach(adr ->{
            List<Double> listY=new ArrayList<Double>();
            HSSFWorkbook wb = null;
            try {
                FileInputStream is = new FileInputStream(adr.get("address").toString());
                POIFSFileSystem fs = new POIFSFileSystem(is);
                wb = new HSSFWorkbook(fs);
            } catch (IOException e) {
                e.printStackTrace();
            }
            HSSFSheet sheet =wb.getSheetAt(0);
            int rowNum = sheet.getPhysicalNumberOfRows();
            for(int i=0;i<rowNum;i++){
                HSSFRow row = sheet.getRow(i);
                listY.add(row.getCell(0).getNumericCellValue());
            }
//            将解析和计算结果放入心电图map中
            adr.put("dotY",listY);
            adr.put("dotX",getArrayX());
        });

        return ra;
    }
}
